<?php
/**
 * 插件管理 - JBlog
 * 
 * @copyright (c) 2008-2010 JBlog (www.lisijie.org)
 * @author lisijie <lisijie86@gmail.com>
 * @version $Id: mod_plugin.php 523 2010-06-23 10:06:24Z lisijie86 $
*/

!defined('IN_ADMIN') && exit('Access Denied!');

if ( !is_founder() ) { prompt(__('对不起，你没有权限执行这项操作。')); }

empty($ac) && $ac = 'inlist';

$baseurl = 'admin.php?ac=plugin';
$plugin_root = JBLOG_ROOT.'plugin';

$enabled = config('plugin_enabled');
if ( !empty($enabled) ) {
	$enabled = unserialize($enabled);
} else {
	$enabled = array();
}

if ( $ac == 'inlist' ) {

	$result = array();
	if ( !empty($enabled) ) {
		foreach ( $enabled as $name => $file ) {
			$result[$name] = parse_plugin_info($plugin_root.DS.$file);
		}
	}
	
} elseif ( $ac == 'unlist' ) {

	$result = array();
	$d = dir($plugin_root);
	while ( false !== ($entry = $d->read()) ) {
		if ( $entry != '.' && $entry != '..' && $entry != '' ) {
			$key = substr($entry, 0, strpos($entry,'.'));
			if ( isset($enabled[$key]) ) continue;
			
			if ( is_dir($plugin_root.DS.$entry) && is_file($plugin_root.DS.$entry.DS.$entry.'.php') ) {
				$result[$entry] = parse_plugin_info($plugin_root.DS.$entry.DS.$entry.'.php');
			} elseif ( strtolower(pathinfo($plugin_root.DS.$entry, PATHINFO_EXTENSION)) == 'php' ) {
				$result[substr($entry,0,-4)] = parse_plugin_info($plugin_root.DS.$entry);
			}
		}
	}
	$d->close();
	
} elseif ( $ac == 'enable' ) {

	$plugin = str_replace('.','', trim($_GET['plugin']));
	if ( !empty($plugin) ) {
		 $initfile = '';
		 if ( is_file($plugin_root.DS.$plugin.'.php') ) {
		 	$initfile = $plugin.'.php';
		 } elseif ( is_file($plugin_root.DS.$plugin.DS.$plugin.'.php') ) {
		 	$initfile = $plugin.DS.$plugin.'.php';
		 }
		if ( $initfile ) {
			if ( !isset($enabled[$plugin]) ) {
				$enabled[$plugin] = $initfile;
			}
			config('plugin_enabled', serialize($enabled));
		}
	}
	
	redirect('?mod=plugin&ac=unlist');
	
} elseif ( $ac == 'disable' ) {

	$plugin = str_replace('.','', trim($_GET['plugin']));
	if ( !empty($plugin) ) {
		if ( isset($enabled[$plugin]) ) {
			unset($enabled[$plugin]);
			config('plugin_enabled', serialize($enabled));
		}
	}
	redirect('?mod=plugin&ac=inlist');
	
}

include admin_tpl('plugin');
?>